/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PhuocPH;


import Entity.*;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author dukynguyen
 */
@Stateless
public class PhuocPHSessionBean implements PhuocPHSessionBeanLocal {
    @PersistenceContext(unitName = "Group05_NationalLevelPaperPresentation-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public boolean InsertCategory(String CaName) {
        try {
            Category objCate = new Category();
            objCate.setCaName(CaName);
            objCate.setIDelete(false);

            persist(objCate);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean DeleteCategory(int CaID) {
        try {
               Category objCate = em.find(Category.class, CaID);
               objCate.setIDelete(true);
               persist(objCate);
               return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean UpdateCategory(int CaID, String CaName) {
        try {
            Category objCate = em.find(Category.class, CaID);
            objCate.setCaName(CaName);
            persist(objCate);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public Entity.Category[] GetCategory() {
        try {
            Query query = em.createNamedQuery("Category.findAll");
             List CateList = query.getResultList();
             Category[] result = new Category[CateList.size()];
             CateList.toArray(result);
             return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Category findCategoryByPK(int CaID) {
         try {
              Category objCate = em.find(Category.class, CaID);
              return objCate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

// ================ Feed Back ==============

    public boolean SendFB(String username, String title, String content) {
        boolean check = false;
        try {
            FeedBack objFB = new FeedBack();
            Account objA = em.find(Account.class, username);
            objFB.setAccount(objA);
            objFB.setFbContent(content);
            objFB.setTitle(title);
            objFB.setDateSend(new Date());
            objFB.setIDelete(false);

            persist(objFB);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean ReplyFB(int id, String reply) {
        boolean check = false;
        try {
            FeedBack objFB = em.find(FeedBack.class, id);
            objFB.setReplyContent(reply);
            objFB.setDateReply(new Date());
            persist(objFB);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public FeedBack findFeedBackByPK(int id) {
        try {
            FeedBack objFB = em.find(FeedBack.class, id);
            return objFB;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Entity.FeedBack[] getFeedBack() {
        try {
            Query query = em.createNamedQuery("FeedBack.findAll");
            List fbList = query.getResultList();
            FeedBack[] result = new FeedBack[fbList.size()];
            fbList.toArray(result);
                return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public AccountFeedBackDTO findFeedBackByAccount(String username) {
        try {
            Query query = em.createQuery("SELECT a FROM Account a WHERE a.username = :username");
            query.setParameter("username", username);
            List listEm =query.getResultList();
            AccountFeedBackDTO objDTO = new AccountFeedBackDTO();
            for (int i= 0; i < listEm.size(); i++) {
              Account objAccount = (Account)listEm.get(i);
              Collection Felist = objAccount.getFeedBackCollection();
              FeedBack[] listFeedBack = new FeedBack[Felist.size()];
              Felist.toArray(listFeedBack);
              objDTO.setObjA(objAccount);
              objDTO.setListFB(listFeedBack);
          }
            return objDTO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//======================== Comment ====================================
    public boolean sendComment(int evID, String user, String content) {
        boolean check = false;
        try {
            Comment objCom = new Comment();
            Event objE = em.find(Event.class, evID);
            Account objA = em.find(Account.class, user);
            objCom.setEvent(objE);
            objCom.setAccount(objA);
            objCom.setComment(content);
            objCom.setDateCreate(new Date());
            objCom.setIDelete(false);

            persist(objCom);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean disableComment(int id) {
        boolean check = false;
        try {
            Comment objCom = em.find(Comment.class, id);
            objCom.setIDelete(true);

            persist(objCom);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean enableComment(int id) {
        boolean check = false;
        try {
            Comment objCom = em.find(Comment.class, id);
            objCom.setIDelete(false);

            persist(objCom);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public EventCommentsDTO CommentsOfEvent(int id) {
        try {
            Query query = em.createQuery("SELECT e FROM Event e WHERE e.evID = :evID");
            query.setParameter("evID", id);
            List listTech =query.getResultList();
            EventCommentsDTO objDTO = new EventCommentsDTO();
            for (int i= 0; i < listTech.size(); i++) {
              Event objT = (Event)listTech.get(i);
              Collection eventComlist = objT.getCommentCollection();
              Comment[] listComment = new Comment[eventComlist.size()];
              eventComlist.toArray(listComment);
              objDTO.setObjE(objT);
              objDTO.setListCom(listComment);
          }
            return objDTO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Entity.Comment[] getComment() {
        try {
            Query query = em.createNamedQuery("Comment.findAll");
            List coList = query.getResultList();
            Comment[] result = new Comment[coList.size()];
            coList.toArray(result);
                return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Comment findCommentByPK(int id) {
        try {
            Comment objco = em.find(Comment.class, id);
            return objco;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public AccountCommentDTO findCommentByAccount(String user) {
        try {
            Query query = em.createQuery("SELECT a FROM Account a WHERE a.username = :username");
            query.setParameter("username", user);
            List listEm =query.getResultList();
            AccountCommentDTO objDTO = new AccountCommentDTO();
            for (int i= 0; i < listEm.size(); i++) {
              Account objAccount = (Account)listEm.get(i);
              Collection colist = objAccount.getCommentCollection();
              Comment[] listComment = new Comment[colist.size()];
              colist.toArray(listComment);
              objDTO.setObjA(objAccount);
              objDTO.setListComment(listComment);
          }
            return objDTO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}
